<?php
namespace App\Admin\Controllers\Products;

use App\Admin\Actions\Products\SkuVariant\ImportSkuVariant;
use App\Admin\Extensions\Grid\Tool\Export;
use App\Models\Basics\AmazonAccount;
use App\Models\Basics\SysCountry;
use App\Models\Basics\SysPlat;
use App\Models\Basics\SysPlatAccount;
use App\Models\Product\ProductClassify;
use App\Models\Product\ProductVariant;
use App\Util\RExcel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Form;
use Illuminate\Support\Facades\Request;

class SkuVariantController extends AdminController{

    protected $title = '产品变体归属管理';

    protected function grid()
    {
        $grid = new Grid(new ProductVariant());
        $grid->column('plat.title','平台');
        $grid->column('country.country','国家');
        $grid->column('classify.title','品类')->style('max-width:200px');
        $grid->column('classify.season','季节性')->using(ProductClassify::$_season)->style('min-width:50px');
        $grid->column('parent_standard','父体是否合规')->bool([1 => true, 2 => false])->style('max-width:200px');
        $grid->column('parent_account','父体账号')->display(function ($value){
            return ProductVariant::formatAccount($value);
        })->style('max-width:255px;word-wrap: break-word;');
        $grid->column('aplus_account','A+账号')->display(function ($value){
            return ProductVariant::formatAccount($value);
        })->style('max-width:255px;word-wrap: break-word;');
        $grid->fixHeader();
        $grid->disableExport();
        $grid->tools(function (Grid\Tools $tools){
            $tools->append(new Export('products/sku-variant-download'));
        });
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            if(!Admin::user()->can('sku.variant.edit')){
                $actions->disableDelete();
                $actions->disableEdit();
            }
        });
        if(Admin::user()->can('sku.variant.edit')){
            $grid->tools(function (Grid\Tools $tools) {
                $tools->append(new ImportSkuVariant());
            });
        }else{
            $grid->disableBatchActions();
            $grid->disableCreateButton();
        }

        $grid->filter(function($filter){
            $filter->column(1/3, function ($filter){
                $filter->equal('plat_id','平台')->select(SysPlat::options());
                $filter->equal('country_id','国家')->select(SysCountry::options());
            });
            $filter->column(1/3, function ($filter) {
                $filter->equal('classify_id', '品类')->select(ProductClassify::options());
                $filter->equal('classify.season', '季节性')->select(ProductClassify::$_season);
            });
            $filter->column(1/3, function ($filter) {
                $filter->equal('parent_standard','合规')->select(ProductVariant::$_parentStandard);
            });
        });
        return $grid;
    }

    protected function form()
    {
        $form = new Form(new ProductVariant());

        $form->tools(function (Form\Tools $tools){
            $tools->disableView();
        });

        $form->hidden('id');
        $form->select('plat_id', '平台')
            ->options(SysPlat::options())
            ->default('1')
            ->rules('required')
            ->required()
            ->setWidth(5);
        $form->select('country_id', '国家')
            ->options(SysCountry::options())
            ->rules('required')
            ->required()
            ->setWidth(5);
        $form->select('classify_id', '品类')
            ->options(ProductClassify::options())
            ->rules('required')
            ->required()
            ->setWidth(5);
        $form->select('parent_standard','父体是否合规')
            ->options(ProductVariant::$_parentStandard)
            ->default('1')
            ->rules('required')
            ->required()
            ->setWidth(5);
        $form->multipleSelect('parent_account','父体账号')
            ->options(SysPlatAccount::options())
            ->rules('required')
            ->required()
            ->setWidth(5);
        $form->multipleSelect('aplus_account','A+账号')
            ->options(SysPlatAccount::options())
            ->rules('required')
            ->required()
            ->setWidth(5);

        return $form;
    }

    public function download(){
        $request = Request::input();
        $query = $this->_initQuery($request);
        $this->_exeDownload($query);
    }

    private function _initQuery($filters): \Illuminate\Database\Eloquent\Builder
    {
        $query = ProductVariant::query()->orderBy('id', 'DESC');
        foreach ($filters as $key => $_filter){
            if($key == 'classify'){
                $query->whereHas('classify', function ($query) use($_filter){
                    $query->where('season', $_filter['season']);
                });
            }
            if(in_array($key,['plat_id','country_id','classify_id','parent_standard'])){
                $query->where($key,$_filter);
            }
        }
        return $query;
    }

    private function _exeDownload($query){
        $head = ['平台','国家','品类','季节性','父体是否合规','父体账号','A+账号'];
        RExcel::exportCsv('产品变体归属列表',function () use ($head, $query) {
            $fp = fopen('php://output', 'w');
            fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF));
            fputcsv($fp, $head);
            $query->chunk(1000, function ($re) use ($fp){
                $data = ProductVariant::getDownloadData($re);
                foreach ($data as $item) {
                    fputcsv($fp, $item);
                }
            });
        });
    }
}
